package animix.blue.server.user.mapper;

import animix.blue.server.common.bean.model.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Arrays;
import java.util.Map;

public class UserProvider {

    public String list(Map<String, Object> para) {
        return new SQL() {{
            SELECT("*");
            FROM("tb_user");
            if (StringUtils.isNotBlank((String) para.get("status"))) {
                WHERE("status in (" + para.get("status") + ")");
            }
            if (StringUtils.isNotBlank((String) para.get("username"))) {
                WHERE("username like '%" + para.get("username") + "%'");
            }
            if (StringUtils.isNotBlank((String) para.get("phone"))) {
                WHERE("phone like '%" + para.get("phone") + "%'");
            }
            if (StringUtils.isNotBlank((String) para.get("email"))) {
                WHERE("email like '%" + para.get("email") + "%'");
            }
            if (StringUtils.isNotBlank((String) para.get("createdSt"))) {
                WHERE("unix_timestamp(created)*1000 >= '" + para.get("createdSt") + "'");
            }
            if (StringUtils.isNotBlank((String) para.get("createdEd"))) {
                WHERE("unix_timestamp(created)*1000 <= '" + para.get("createdEd") + "'");
            }
            if (StringUtils.isNotBlank((String) para.get("sorter"))) {
                String[] sorter = ((String) para.get("sorter")).split("_");
                ORDER_BY(sorter[1].equals("descend") ? sorter[0] + " DESC" : sorter[0] + " ASC");
            }
        }}.toString();
    }

    public String updateByIdSelective(User user) {
        return new SQL() {{
            UPDATE("tb_user");
            if (user.getUsername() != null) {
                SET("username = #{username}");
            }
            if (user.getPassword() != null) {
                SET("password = #{password}");
            }
            if (user.getPhone() != null) {
                SET("phone = #{phone}");
            }
            if (user.getEmail() != null) {
                SET("email = #{email}");
            }
            if (user.getStatus() != null) {
                SET("status = #{status}");
            }
            if (user.getCreated() != null) {
                SET("created = #{created}");
            }
            if (user.getUpdated() != null) {
                SET("updated = #{updated}");
            }
            WHERE("id = #{id}");
        }}.toString();
    }

    public String updateByIdsSelective(@Param("ids") Integer[] ids, @Param("user") User user) {
        return new SQL() {{
            UPDATE("tb_user");
            if (user.getUsername() != null) {
                SET("username = #{user.username}");
            }
            if (user.getPassword() != null) {
                SET("password = #{user.password}");
            }
            if (user.getPhone() != null) {
                SET("phone = #{user.phone}");
            }
            if (user.getEmail() != null) {
                SET("email = #{user.email}");
            }
            if (user.getStatus() != null) {
                SET("status = #{user.status}");
            }
            if (user.getCreated() != null) {
                SET("created = #{user.created}");
            }
            if (user.getUpdated() != null) {
                SET("updated = #{user.updated}");
            }
            WHERE("id in (" + StringUtils.strip(Arrays.toString(ids), "[]") + ")");
        }}.toString();
    }
}
